package cn.iocoder.boot.service.impl;

import cn.iocoder.boot.entity.ComponentPrice;
import cn.iocoder.boot.mapper.ComponentPriceMapper;
import cn.iocoder.boot.mapper.ComponentStockMapper;
import cn.iocoder.boot.service.ComponentPriceService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jsoup.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组件价格Service实现类
 *
 * @author 芋道源码
 */
@Service
public class ComponentPriceServiceImpl implements ComponentPriceService {

    @Autowired
    private ComponentPriceMapper componentPriceMapper;

    @Autowired
    private ComponentStockMapper componentStockMapper;

    @Override
    public CommonResult<Boolean> createComponentPrice(ComponentPrice componentPrice) {
        boolean result = componentPriceMapper.insert(componentPrice) > 0;
        return CommonResult.success(result);
    }

    @Override
    public CommonResult<Boolean> createComponentPriceBranch(List<ComponentPrice> componentPrice) {
        componentPriceMapper.insert(componentPrice);
        return CommonResult.success(true);
    }


    @Override
    public CommonResult<Boolean> updateComponentPrice(ComponentPrice componentPrice) {
        boolean result = componentPriceMapper.updateById(componentPrice) > 0;
        return CommonResult.success(result);
    }

    @Override
    public CommonResult<Boolean> deleteComponentPrice(Long id) {
        boolean result = componentPriceMapper.deleteById(id) > 0;
        return CommonResult.success(result);
    }

    @Override
    public CommonResult<Boolean> deleteByTask() {
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();

        // 先查询所有需要判断的记录
        List<ComponentPrice> prices = componentPriceMapper.selectList(Wrappers.<ComponentPrice>lambdaQuery()
                .lt(ComponentPrice::getExpireDate, currentTime));

        if (prices != null && !prices.isEmpty()) {

            // 收集需要删除的ID
            List<Long> idsToDelete = prices.stream()
                    .map(ComponentPrice::getId)
                    .collect(Collectors.toList());

            // 如果有需要删除的记录
            if (!idsToDelete.isEmpty()) {
                // 批量删除
                int deletedCount = componentPriceMapper.deleteBatchIds(idsToDelete);
                return CommonResult.success(deletedCount > 0);
            }
        }

        return CommonResult.success(false);
    }


    @Override
    public CommonResult<ComponentPrice> getComponentPrice(Long id) {
        ComponentPrice componentPrice = componentPriceMapper.selectById(id);
        return CommonResult.success(componentPrice);
    }

    @Override
    public CommonResult<List<ComponentPrice>> listComponentPrices(List<Long> id, String currency) {
        QueryWrapper<ComponentPrice> queryWrapper = new QueryWrapper<>();
        if (id != null && !id.isEmpty()) {
            queryWrapper.in("stock_id", id);
        }

        if (!StringUtil.isBlank(currency)) {
            queryWrapper.eq("currency", currency);
        }


        List<ComponentPrice> componentPrices = componentPriceMapper.selectList(queryWrapper);

        return CommonResult.success(componentPrices);
    }

    @Override
    public CommonResult<List<ComponentPrice>> listComponentPricesForTime(List<String> id, String currency) {
        QueryWrapper<ComponentPrice> queryWrapper = new QueryWrapper<>();
        if (id != null && !id.isEmpty()) {
            queryWrapper.in("unique_id", id);
        }
        if (!StringUtil.isBlank(currency)) {
            queryWrapper.eq("currency", currency);
        }
        queryWrapper.ge("expire_date", LocalDateTime.now());
        List<ComponentPrice> componentPrices = componentPriceMapper.selectList(queryWrapper);
        return CommonResult.success(componentPrices);
    }

    @Override
    public void createComponentPriceOfUniqueKey(ComponentPrice componentPrice) {
        componentPriceMapper.createComponentPriceOfUniqueKey(componentPrice);
    }
}
